package com.wanmait.recycle.webcontroller;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.service.UserinfoService;
import com.wanmait.recycle.util.CookieUtil;
import com.wanmait.recycle.util.EmailSend;
import com.wanmait.recycle.util.IPUtil;
import com.wanmait.recycle.util.ValidateCode;

@Controller
@RequestMapping("web")
public class WebUserinfoController {

	@Resource
	private UserinfoService userinfoService;
	
	//进入登录页面
	@RequestMapping(value="userLogin/login",method=RequestMethod.GET)
	public void login() {
		
	}
	//进入注册页面
	@RequestMapping(value="userLogin/reg",method=RequestMethod.GET)
	public void reg() {
		
	}
	
	//页面获得验证码
	@RequestMapping("code")
	@ResponseBody
	public String getCode(HttpSession session) {
		return (String)session.getAttribute("code");	
	}
	
	//验证码图片
	@RequestMapping("setValidateCode")
	public void validateCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
		ValidateCode vCode = new ValidateCode();
		String code = vCode.getCode();			
		HttpSession session = request.getSession();
		session.setAttribute("code", code);		
		OutputStream out = response.getOutputStream();
		vCode.write(out);		
	}
	
	//登录
	@RequestMapping(value="login",method=RequestMethod.POST)
	public String logIn(Userinfo userinfo,String validatecode,String rememberMe,HttpServletRequest request,HttpSession session,HttpServletResponse response) {
		
		session.setAttribute("userinfoName", userinfo.getName());
		//获得session里的验证码
		String code = (String)session.getAttribute("code");
		if(!code.equalsIgnoreCase(validatecode)) return "redirect:/web/userLogin/login?msg=code";
		
		Userinfo user = userinfoService.findUserinfoByExample(userinfo);
		
		if(user==null) return "redirect:/web/userLogin/login?msg=user";
		
		session.setAttribute("userinfo", user);
		session.removeAttribute("userinfoName");
		//用户登录后记录登录时间和登录Ip
		user.setLastIP(IPUtil.getIpAddr(request));
		user.setLastTime(new Timestamp(System.currentTimeMillis()));
		userinfoService.updateIp(user);
		
	
		if(rememberMe!=null)
		{
			//写Cookie
			CookieUtil.addCookie("username", user.getName(), "/", 60*60*24*30, response);
			CookieUtil.addCookie("password", user.getPassword(), "/", 60*60*24*30, response);
		}
		
		String lastUrl = (String)session.getAttribute("lastUrl");
		
		if(lastUrl==null) {
			lastUrl = "/index";
		}
		return "redirect:"+lastUrl;
	}
	
	
	//页面获得验证码
		@RequestMapping("mailCode")
		@ResponseBody
		public String getmailCode(HttpSession session) {
			return (String)session.getAttribute("mailcode");	
		}
	
	//验证邮箱
	@RequestMapping("checkedMail")
	@ResponseBody
	public boolean checked(String mailBox,HttpSession session) {
		boolean flag = true;
		try {
			EmailSend emailSend = EmailSend.getEmailSend();
			String mailcode = emailSend.sendEmail(mailBox);	
			session.setAttribute("mailcode", mailcode);
			flag = true;
		} catch (Exception e) {
			flag = false;
		}
		
		return flag;
		
	}
	
	//注册
	@RequestMapping("regiest")
	public String regiest(Userinfo userinfo,String mailBoxCode,HttpSession session) {
		
		String code =(String)session.getAttribute("mailcode");
		if(!mailBoxCode.equalsIgnoreCase(code)) return "redirect:/web/userLogin/reg?msg=code";	
		boolean flag = userinfoService.isUserinfoByExample(userinfo);
		
		if(flag) return "redirect:/web/userLogin/reg?msg=user";
		
		
		userinfoService.saveUserinfo(userinfo);
		Userinfo user = userinfoService.findUserinfoByExample2(userinfo);
		System.out.println(user.getName());
		session.setAttribute("userinfo", user);
		return "redirect:/index";
		
	}
	
	//退出
	@RequestMapping("exit")
	public String  exit(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		//删除userinfo属性
		session.removeAttribute("userinfo");
		//销毁session
		
		//删除cookie
		CookieUtil.addCookie("username", "", "/", 0, response);
		CookieUtil.addCookie("password", "", "/", 0, response);
		return "redirect:/index";
		
	}
	
	@RequestMapping("userinfo/kkkk")
	public void kkkk() {
		
	}
	
	@RequestMapping("userinfo/isMail")
	@ResponseBody
	public boolean isMail(Userinfo userinfo) {
		boolean f = userinfoService.isUserinfoByExample(userinfo);
		
		return f;
	}
	@RequestMapping("userinfo/isName")
	@ResponseBody
	public boolean isName(Userinfo userinfo) {
		
		boolean f = userinfoService.isUserinfoByExample(userinfo);
		
		return f;
	}
	
	@RequestMapping("userinfo/changeUserinfo")
	@ResponseBody
	public void changeUserinfo(HttpSession session) {
		System.out.println("**************");
		Userinfo user = (Userinfo)session.getAttribute("userinfo");
		Userinfo userinfo = userinfoService.findUserinfoByExample2(user);
		session.setAttribute("userinfo", userinfo);
		
	}
	
}











